var fs = require("fs");
var defaultResJsonPath = "../GameHall/resource/default.res.json";
var resGroupConfig = "../GameHall/resource/config/resGroupConfig.json";

exports.writeResult = function(obj) {
    fs.writeFile(resGroupConfig, JSON.stringify(obj, null, 4), function () {
        console.log("write resGroupConfig  success!");
    });

    //写入default.res.json
    var defaultJson = JSON.parse(fs.readFileSync(defaultResJsonPath));
    var groups = defaultJson.groups;
    var preGroups = [];
    for (var i=0; i<groups.length; i++) {
        if(groups[i].name.indexOf("m_") != 0 && groups[i].name.indexOf("pc_") != 0 ) {
            preGroups.push(groups[i]);
        }
    }
    defaultJson.groups = preGroups.concat(obj.groups);
    fs.writeFile(defaultResJsonPath, JSON.stringify(defaultJson, null, 4), function () {
        console.log("write default.res.json  success!");
    });

    //将配置写入ResGroups.ts  取消这个写入，改为写成json的配置文件
    // var tsFile = fs.readFileSync(resGroupTs);
    // var index = tsFile.indexOf("public static MGroups");
    // var file0 = tsFile.slice(0,index);
    // var file1 = tsFile.slice(index);
    // var file2 = file1.slice(file1.indexOf("}")+1);
    // var file3 = file2.slice(file2.indexOf("}")+1);
    // var result = file0
    //     + "public static MGroups = "+"\n"
    //     + JSON.stringify(obj.mViews, null, 4)+"\n"
    //     + "\t\t/**PC版的每个view的多资源组 */"+"\n"
    //     + "\t\tpublic static PCGroups ="+"\n"
    //     + JSON.stringify(obj.pcViews, null, 4)+"\n"
    //     + file3;
    // fs.writeFile(resGroupTs, result, function () {
    //     console.log("write ResGroups  success!");
    // });
}
